
Norm.Transaction = function(fn, bind) {
	
	try {
		if (Norm.Transaction.depth <= 0) {
			Norm.Database.execute("BEGIN");
		}
		Norm.Transaction.depth++;
		fn.apply(bind || this, []);
	} catch(e) {
		Norm.Transaction.depth = 0;
		Norm.Database.execute("ROLLBACK");
		throw e;
	}
	Norm.Transaction.depth = Math.max(0, Norm.Transaction.depth - 1);
	if (Norm.Transaction.depth <= 0) {
		Norm.Database.execute("COMMIT");
	}
};

Norm.Transaction.depth = 0;
